feat(rpc): add debug_intermediateRoots#11524
Conversation
|
@claude review |
|
Claude finished @svlachakis's task in 3m 28s —— View job PR Review:
|
| IntermediateRootsBlockTracer tracer = new(scope.Component.WorldState); | ||
| scope.Component.BlockchainProcessor.Process(block, ProcessingOptions.Trace, tracer.WithCancellation(cancellationToken), cancellationToken); | ||
|
|
||
| return tracer.BuildResult(); |
There was a problem hiding this comment.
Low — inconsistent exception-safety pattern
All other block processing paths in GethStyleTracer guard the tracer with a try/catch + tracer.TryDispose() call on exception (see TraceBlockImpl, Trace(long, Transaction, ...), etc.). IntermediateRootsBlockTracer is not currently IDisposable, so this doesn't leak today — but the pattern exists precisely so that tracers can become disposable without requiring every call site to be updated.
Suggested fix:
IntermediateRootsBlockTracer tracer = new(scope.Component.WorldState);
try
{
scope.Component.BlockchainProcessor.Process(block, ProcessingOptions.Trace, tracer.WithCancellation(cancellationToken), cancellationToken);
return tracer.BuildResult();
}
catch
{
tracer.TryDispose();
throw;
}There was a problem hiding this comment.
Good point on the consistency — wrapped it in a160e9f1. If this tracer grows into being disposable later, the call site won't need to be remembered separately.
| IReadOnlyCollection<GethLikeTxTrace> TraceBlock(BlockParameter blockParameter, GethTraceOptions options, CancellationToken cancellationToken); | ||
| IReadOnlyCollection<GethLikeTxTrace> TraceBlock(Rlp blockRlp, GethTraceOptions options, CancellationToken cancellationToken); | ||
| IReadOnlyCollection<GethLikeTxTrace> TraceBlock(Block block, GethTraceOptions options, CancellationToken cancellationToken); | ||
| IReadOnlyCollection<Hash256> TraceBlockIntermediateRoots(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken); |
There was a problem hiding this comment.
Low — missing XML doc
Per the project's coding style, all public API members should have <summary> documentation. Every other method in this interface currently lacks docs too, but since this is new surface area it's a good opportunity to set the standard.
/// <summary>Replays <paramref name="blockHash"/> and returns the world-state root after each transaction, in execution order.</summary>
IReadOnlyCollection<Hash256> TraceBlockIntermediateRoots(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken);There was a problem hiding this comment.
Added on the interface in 598cf9b1, plus a more descriptive JsonRpcMethod attribute alongside it.
|
|
||
| actual.Result.ResultType.Should().Be(ResultType.Failure); | ||
| actual.ErrorCode.Should().Be(ErrorCodes.ResourceUnavailable); | ||
| } |
There was a problem hiding this comment.
Medium — missing integration test + Low — missing "block not found" RPC test
Two gaps in coverage:
-
Integration path: Both existing tests mock
IDebugBridgeentirely. TheIntermediateRootsBlockTracer.Capturelogic — particularly theworldState.RecalculateStateRoot()fallback for post-Byzantium blocks — is never exercised. Adding a test that drivesTraceBlockIntermediateRootsthroughTestBlockchain(as other tracer tests do) would give confidence that the root accumulation across transactions is correct. -
Header-not-found path:
StandardTraceBlockToFile_returns_error_when_missing_blockexists fordebug_standardTraceBlockToFilebut the equivalent test is absent fordebug_intermediateRoots. Consider adding:
[Test]
public void Debug_intermediateRoots_fails_when_block_not_found()
{
Hash256 blockHash = TestItem.KeccakA;
_blockFinder.FindHeader(blockHash).ReturnsNull();
DebugRpcModule rpcModule = CreateDebugRpcModule(_debugBridge);
ResultWrapper<IReadOnlyCollection<Hash256>> actual = rpcModule.debug_intermediateRoots(blockHash);
actual.Result.ResultType.Should().Be(ResultType.Failure);
actual.ErrorCode.Should().Be(ErrorCodes.ResourceNotFound);
}There was a problem hiding this comment.
Block-not-found unit test landed in 8258b4fa. For the integration path, wired up a TestRpcBlockchain-driven test in 74b08eb2 that replays a real two-tx block and checks the roots are non-zero and distinct — and writing that test actually surfaced a real bug in the post-Byzantium fallback (details in the summary comment, fix in b3290a83). Glad it was flagged.
|
please fix claude marked issues |
|
@claude please review also the following findings.
GethStyleTracer.cs · TraceBlockIntermediateRoots — Genesis NRE. FindParent(block) returns null for genesis (GethStyleTracer.cs:224) and is passed straight into DebugRpcModule.cs · debug_intermediateRoots catch — InvalidOperationException is mapped to ResourceNotFound, but it's raised for "block not found", "parent hash
GethStyleTracer.cs · bad-block fallback — geth's debug_intermediateRoots (#23594) was designed for diagnosing client divergence on bad blocks; it falls back to IntermediateRootsBlockTracer.cs · partial-result semantics — geth returns roots[0..i] with nil error on mid-block tx failure (the diagnostic use case). Behavior
DebugModuleTests.cs — both new tests just verify the bridge is called. The "matches geth" claim hinges on these, none of which are covered:
IntermediateRootsBlockTracer.cs:7 — unused using Nethermind.Evm.TransactionProcessing;. IntermediateRootsBlockTracer.cs · StateRoot = Keccak.Zero — dead initializer; Capture always overwrites before OnEnd reads (IsTracingReceipt is hardcoded true). IDebugRpcModule.cs · JsonRpcMethod description — spec it: "post-tx state root, in execution order; excludes EIP-4788/2935 system calls and withdrawals; cost |
|
Claude finished @svlachakis's task in 3m 51s —— View job Review: additional findings on
|
| ArgumentNullException.ThrowIfNull(options); | ||
|
|
||
| Block block = blockTree.FindBlock(blockHash) ?? throw new InvalidOperationException($"Cannot find block {blockHash}"); | ||
| BlockHeader? parent = FindParent(block); |
There was a problem hiding this comment.
High — Genesis NRE / wrong behaviour
FindParent returns null when block.IsGenesis (see line 240: the body is skipped and null is returned). That null is then passed to BuildAndOverride on line 135. Whether this produces an NRE or silent wrong output depends on BuildAndOverride's implementation, but either way the behaviour is incorrect — geth explicitly rejects genesis with "genesis is not traceable".
Add a guard before the FindParent call:
| BlockHeader? parent = FindParent(block); | |
| Block block = blockTree.FindBlock(blockHash) ?? throw new InvalidOperationException($"Cannot find block {blockHash}"); | |
| if (block.IsGenesis) throw new InvalidOperationException("genesis is not traceable"); | |
| BlockHeader? parent = FindParent(block); |
There was a problem hiding this comment.
Nice catch. Added an explicit block.IsGenesis guard with the same wording geth uses, before FindParent runs. a160e9f1.
| catch (InvalidOperationException ex) | ||
| { | ||
| return ResultWrapper<IReadOnlyCollection<Hash256>>.Fail(ex.Message, ErrorCodes.ResourceNotFound); | ||
| } |
There was a problem hiding this comment.
High — Overly broad exception mapping
InvalidOperationException is thrown for at least three distinct conditions: "cannot find block" (missing from tree), "cannot trace blocks with invalid parents" (bad chain state), and — once the genesis guard is added — "genesis is not traceable". All three land on ErrorCodes.ResourceNotFound, which is wrong for the latter two.
Compare to the existing debug_standardTraceBlockToFile, which does not catch InvalidOperationException — the RPC framework maps unhandled exceptions to an internal error, which is at least not misleading. Either remove the catch block entirely (matching that pattern), or introduce typed exceptions (BlockNotFoundException, TraceNotSupportedException) and map them individually.
There was a problem hiding this comment.
Agreed it was too coarse — dropped the catch in 4b194fbe. Block-not-found is already covered by TryGetHeaderAndCheckState upstream, and the other paths (bad parent, genesis) shouldn't be masquerading as ResourceNotFound.
| ArgumentNullException.ThrowIfNull(blockHash); | ||
| ArgumentNullException.ThrowIfNull(options); | ||
|
|
||
| Block block = blockTree.FindBlock(blockHash) ?? throw new InvalidOperationException($"Cannot find block {blockHash}"); |
There was a problem hiding this comment.
Medium — No bad-block fallback (compat gap vs geth)
blockTree.FindBlock(blockHash) only searches canonical storage. Geth's debug_intermediateRoots (ethereum/go-ethereum#23594) was specifically designed for diagnosing client divergence on bad blocks — blocks that were received and rejected — so it also checks rawdb.ReadBadBlock.
GethStyleTracer already has badBlockStore injected and used in TraceBadBlockToFile. This path should either:
- Also check
badBlockStore.GetAll()on lookup miss, or - Open a follow-up issue and add a
// TODO:comment here so the gap is tracked explicitly.
There was a problem hiding this comment.
Added in a160e9f1 — canonical lookup first, then badBlockStore.GetAll().FirstOrDefault(...) before throwing, same shape as TraceBadBlockToFile.
| public override void MarkAsSuccess(Address recipient, in GasConsumed gasSpent, byte[] output, LogEntry[] logs, Hash256? stateRoot = null) => | ||
| Capture(stateRoot); | ||
|
|
||
| public override void MarkAsFailed(Address recipient, in GasConsumed gasSpent, byte[] output, string? error, Hash256? stateRoot = null) => | ||
| Capture(stateRoot); |
There was a problem hiding this comment.
Medium — Partial-result semantics unspecified and untested
If a transaction fails mid-block (e.g. an OOG reverted tx), MarkAsFailed is still called and a root is still appended — so the count of returned roots always equals the number of transactions regardless of success/failure. This is the correct and intended semantics, but:
- It is not documented: the
JsonRpcMethoddescription and XML docs don't mention whether failed txs contribute a root. - It is not tested: there's no test with a mixed success/failure block that verifies the final count and the state after a failed tx.
Geth returns roots[0..i] with nil error when a tx fails mid-block (the exact diagnostic use case). Add a test asserting that a block with one successful tx and one OOG tx returns exactly two roots, and the second root equals the state root after the failed tx's gas refund.
There was a problem hiding this comment.
Documented the behaviour in the tracer's XML <remarks> (5d39af79) and the JsonRpcMethod description (598cf9b1). Didn't add a dedicated failed-tx test in this round — wants an OOG fixture wired up. Can layer it in here if you'd rather not defer, otherwise will track separately.
| using Nethermind.Core.Crypto; | ||
| using Nethermind.Evm.State; | ||
| using Nethermind.Evm.Tracing; | ||
| using Nethermind.Evm.TransactionProcessing; |
There was a problem hiding this comment.
Low — Unused using
Nethermind.Evm.TransactionProcessing is not referenced anywhere in this file. Remove it.
| using Nethermind.Evm.TransactionProcessing; |
There was a problem hiding this comment.
Left this one in — GasConsumed lives in Nethermind.Evm.TransactionProcessing and it's the type in the MarkAsSuccess/MarkAsFailed signatures. Tried removing it and the build went red.
| { | ||
| public override bool IsTracingReceipt => true; | ||
|
|
||
| public Hash256 StateRoot { get; private set; } = Keccak.Zero; |
There was a problem hiding this comment.
Low — Dead initializer
IsTracingReceipt is hardcoded true, so MarkAsSuccess or MarkAsFailed is guaranteed to run before OnEnd reads StateRoot. The = Keccak.Zero initializer is never observable and misleads readers into thinking a tx could exit with Keccak.Zero (it never can). Remove it or replace with just the auto-property declaration.
| public Hash256 StateRoot { get; private set; } = Keccak.Zero; | |
| public Hash256 StateRoot { get; private set; } |
| [JsonRpcMethod(Description = "Similar to debug_traceBlock, this method accepts a block hash and replays the block that is already present in the database.", IsImplemented = true, IsSharable = false)] | ||
| ResultWrapper<IReadOnlyCollection<GethLikeTxTrace>> debug_traceBlockByHash(Hash256 blockHash, GethTraceOptions options = null); | ||
|
|
||
| [JsonRpcMethod(Description = "Replays the block already present in the database and returns the world state root after each transaction.", IsImplemented = true, IsSharable = false)] |
There was a problem hiding this comment.
Low — Weak JsonRpcMethod description
The current description omits details that callers need to understand the contract. Geth's docs are equally sparse. Suggested richer description:
| [JsonRpcMethod(Description = "Replays the block already present in the database and returns the world state root after each transaction.", IsImplemented = true, IsSharable = false)] | |
| [JsonRpcMethod(Description = "Replays a block and returns the world-state root after each transaction in execution order. EIP-4788/EIP-2935 system calls and withdrawals do not produce an entry. Cost scales O(txs × state_diff). Block must be canonical or in the bad-block store.", IsImplemented = true, IsSharable = false)] |
There was a problem hiding this comment.
Rewrote it in 598cf9b1 to spell out the contract: replay semantics, system-call/withdrawal exclusion, partial-result behaviour, and the canonical-or-bad-block lookup.
|
|
||
| actual.Result.ResultType.Should().Be(ResultType.Failure); | ||
| actual.ErrorCode.Should().Be(ErrorCodes.ResourceUnavailable); | ||
| } |
There was a problem hiding this comment.
Medium — Critical test scenarios missing
Both current tests mock IDebugBridge at the boundary — the "matches geth" claim is unverified. The following scenarios are completely uncovered:
| Scenario | Why it matters |
|---|---|
| N txs in → exactly N roots out | Basic contract verification |
| Genesis hash → clean error (not NRE) | Once genesis guard is added |
Block-not-found → ResourceNotFound |
Exercises FindHeader null path |
| Failed tx still produces a root | Partial-result semantics |
| System calls (EIP-4788/2935) don't add entry | Spec correctness |
| Withdrawals don't add entry | Spec correctness |
| Cancellation → correct error | Parity with other trace methods |
| Root equals hand-computed post-tx stateRoot | Correctness against known reference |
At minimum, add a TestBlockchain-based integration test (similar to those in GethStyleTracerTests) that replays a two-transaction block and asserts the two returned roots are distinct and non-zero. The block-not-found and genesis error tests can stay at the DebugRpcModule unit level.
There was a problem hiding this comment.
Integration test in 74b08eb2 covers count, distinctness, non-zero, plus the genesis rejection. Unit-level block-not-found in 8258b4fa. The remaining four (system-call/withdrawal absence, failed-tx still producing a root, hand-computed reference, cancellation) need richer fixtures — would rather not bolt them on hastily. Happy to either pull them into this PR or track in a follow-up; let me know which you prefer.
…Roots Match geth: reject genesis with a clear error and fall back to the bad-block store when the canonical lookup misses, so the diagnostic use case (replaying a rejected block) works. Wrap processing in try/catch+TryDispose to mirror the other tracer entry points.
Drop the unobservable Keccak.Zero initializer (IsTracingReceipt is hardcoded true, so MarkAsSuccess/MarkAsFailed always runs before OnEnd reads StateRoot) and document partial-result behaviour: failed transactions still produce a root, system calls (EIP-4788, EIP-2935) and withdrawals do not.
…termediateRoots The catch mapped every InvalidOperationException to ResourceNotFound, which is wrong for non-existence errors like "genesis is not traceable" or "cannot trace blocks with invalid parents". Match the debug_standardTraceBlockToFile pattern and let the framework map unhandled exceptions to an internal error instead of misclassifying them.
Spell out the contract on the public API: post-tx roots in execution order, system calls and withdrawals excluded, failed txs still produce a root, canonical or bad-block lookup.
Mirrors StandardTraceBlockToFile_returns_error_when_missing_block: when the block header lookup misses, the RPC must return ResourceNotFound rather than relying on the bridge to throw.
TransactionProcessor commits with commitRoots=false on EIP-658 chains, which buffers account/storage changes into _blockChanges without flushing them to the trie. RecalculateStateRoot then re-derives the root from an unchanged trie, yielding the parent block's root for every transaction. Plumb the active IReleaseSpec into IntermediateRootsBlockTracer and force a commitRoots=true flush before reading the root. The BlockProcessor's later end-of-block commit is idempotent for already-flushed entries (Before=After short-circuit in FlushToTree).
Replay a real two-transaction block through TestRpcBlockchain and assert the returned roots: one per transaction, all non-zero, all distinct. Also assert that calling the RPC on the genesis hash surfaces the "genesis is not traceable" error from GethStyleTracer rather than returning a misleading result.
|
Pushed 7 commits addressing the review.
One thing worth flagging separately: while writing the integration test I noticed the post-Byzantium fallback was returning the parent block's root for every transaction. Two coverage items from the critical-scenarios thread that I didn't ship: a failed-tx-still-produces-a-root assertion and a hand-computed reference root. Both want richer fixtures — happy to pull them into this PR or track in a follow-up, whichever you prefer. |
|
@claude re-review |
|
Claude finished @LukaszRozmej's task in 4m 49s —— View job Re-review:
|
| Finding | Resolution |
|---|---|
| H Genesis NRE | Guard added before FindParent — throws InvalidOperationException("genesis is not traceable") |
H Over-broad InvalidOperationException catch |
Dropped entirely in 4b194fb; block-not-found already handled upstream by TryGetHeaderAndCheckState |
| M No bad-block fallback | Added in a160e9f1 — canonical first, then badBlockStore.GetAll().FirstOrDefault(...) |
| M Partial-result semantics undocumented | <remarks> on tracer class + JsonRpcMethod description both updated |
L Missing try/catch + TryDispose() |
Added in a160e9f1 |
L Missing XML doc on IGethStyleTracer |
Added in 598cf9b1 |
L Weak JsonRpcMethod description |
Replaced with spec-accurate description in 598cf9b1 |
L Dead = Keccak.Zero initializer |
Removed in 5d39af79 |
L "Unused" using Nethermind.Evm.TransactionProcessing |
Developer correctly kept it — GasConsumed (used in method overrides) is in that namespace |
| Bug Post-Byzantium roots all equal parent root | Fixed in b3290a83: IReleaseSpec injected into tracer; worldState.Commit(spec, commitRoots: true) called per-tx to flush before RecalculateStateRoot |
| Test Block-not-found unit test | Added in 8258b4fa |
| Test Integration test | 74b08eb2 — two-tx block via TestRpcBlockchain, asserts two distinct non-zero roots + genesis rejection |
Remaining findings
Medium
Genesis throws unhandled exception at the RPC level — DebugRpcModule.cs:301
TryGetHeaderAndCheckState passes for genesis (genesis header is found; genesis state is available). The InvalidOperationException("genesis is not traceable") thrown inside GethStyleTracer propagates through DebugBridge.GetBlockIntermediateRoots with no handler in debug_intermediateRoots. In a live node, RPC callers receive a generic internal-error response (−32603) instead of a client-meaningful one. The integration test explicitly tests that the exception is thrown (act.Should().Throw<InvalidOperationException>()), so this is intentional — but it is inconsistent with how the same method handles block-not-found (→ ResourceNotFound) and state-unavailable (→ ResourceUnavailable).
debug_traceBlockByHash has the same gap for genesis (it would throw internally too), so this is pattern-consistent — but the genesis guard here is an explicit, known rejection case, which makes it straightforward to map:
try
{
IReadOnlyCollection<Hash256> roots = debugBridge.GetBlockIntermediateRoots(blockHash, cancellationToken, options);
if (_logger.IsTrace) _logger.Trace($"...");
return ResultWrapper<IReadOnlyCollection<Hash256>>.Success(roots);
}
catch (InvalidOperationException e) when (e.Message.Contains("genesis"))
{
return ResultWrapper<IReadOnlyCollection<Hash256>>.Fail(e.Message, ErrorCodes.InvalidInput);
}Or better: introduce a GenesisNotTraceableException in the tracer and catch that specifically.
Medium (explicitly acknowledged, deferred)
Test coverage gaps — DebugRpcModuleTests.IntermediateRoots.cs
Developer explicitly acknowledged these are not in this PR and offered to add or defer:
- Failed tx still produces a root (partial-result semantics test)
- System-call/withdrawal exclusion verified at the integration level
- Hand-computed reference root vs known-good state
- Cancellation produces correct error
These are documented as a known gap — not blocking if reviewers accept the deferral.
Low
LINQ in bad-block lookup — GethStyleTracer.cs:135
badBlockStore.GetAll().FirstOrDefault(b => b.Hash == blockHash) uses LINQ. The no-LINQ style rule applies, but this is identical to the existing pattern in TraceBadBlockToFile in the same file. The inconsistency is pre-existing; a nit at most.
What looks good
- Post-Byzantium root fix (
b3290a83) is non-trivial and correct: callingworldState.Commit(spec, commitRoots: true)mid-block (afterMarkAsSuccess/MarkAsFailed, when the tx is fully committed) flushes the journal to the trie soRecalculateStateRootsees this transaction's effect. The end-of-block commit is idempotent. The inline comment explains the invariant clearly. - Bad-block fallback mirrors
TraceBadBlockToFileexactly — consistency is good. - Genesis guard fires before
FindParent, eliminating the NRE. - Interface XML doc is precise and spec-faithful (partial-result semantics, exclusions, canonical-or-bad-block lookup all documented).
- Test structure — integration tests live in a new partial class file following the established
DebugRpcModuleTests.*.csconvention;CreateTraceBlockTransactionsis reused from the trace-block suite.
Verdict: not mergeable — one new Medium finding (genesis exception propagates as unhandled instead of a clean error response) that was not present or acknowledged in prior comments.
|
|
||
| if (_logger.IsTrace) _logger.Trace($"{nameof(debug_intermediateRoots)} request {blockHash}, roots: {roots.Count}"); | ||
|
|
||
| return ResultWrapper<IReadOnlyCollection<Hash256>>.Success(roots); |
There was a problem hiding this comment.
Medium — Genesis throws unhandled exception
TryGetHeaderAndCheckState passes for genesis (the header exists and state is available), so the InvalidOperationException("genesis is not traceable") thrown by GethStyleTracer propagates unhandled to the RPC framework — clients get a −32603 internal error instead of a meaningful response. All other invalid-input conditions on this method return clean ResourceNotFound / ResourceUnavailable wrappers.
| return ResultWrapper<IReadOnlyCollection<Hash256>>.Success(roots); | |
| IReadOnlyCollection<Hash256> roots; | |
| try | |
| { | |
| roots = debugBridge.GetBlockIntermediateRoots(blockHash, cancellationToken, options); | |
| } | |
| catch (InvalidOperationException e) | |
| { | |
| return ResultWrapper<IReadOnlyCollection<Hash256>>.Fail(e.Message, ErrorCodes.InvalidInput); | |
| } |
(Or introduce a typed GenesisNotTraceableException and catch that specifically to avoid catching unrelated InvalidOperationExceptions from deeper in the stack.)
Fixes #3456
Changes
IntermediateRootsBlockTracer(Nethermind.Blockchain.Tracing) — captures the world state root after each transaction. Falls back toIWorldState.RecalculateStateRoot()post-Byzantium whereTransactionProcessorno longer pre-computes the root.IGethStyleTracer.TraceBlockIntermediateRoots(blockHash, options, ct)and the matchingGethStyleTracerimplementation, mirroring the existingTraceBlockpipeline (parent header, scoped world state,BlockchainProcessor.Process).IDebugBridge.GetBlockIntermediateRootsplumbing.debug_intermediateRoots(Hash256 blockHash, GethTraceOptions? options = null)onIDebugRpcModule/DebugRpcModule. Behaviour matches geth (eth/tracers: implement debug.intermediateRoots ethereum/go-ethereum#23594): one root per transaction, in execution order.DebugModuleTestscover the success path and the no-state-available failure path.Types of changes
Testing
Requires testing
If yes, did you write tests?
Documentation
Requires documentation update
Requires explanation in Release Notes
Adds the
debug_intermediateRootsJSON-RPC method, which replays a block and returns the world state root after each transaction in execution order. Matches the equivalent geth endpoint.